package com.zyl.basewidget;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

public class BaseConf extends Activity {
	int mAppWidgetId = 0;

	DatePicker datePicker;
	EditText title;

	Button submitButton;
	Button cancelButton;

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setResult(0);
		setContentView(R.layout.configure);
		init();
		Bundle localBundle = getIntent().getExtras();
		if (localBundle != null)
			this.mAppWidgetId = localBundle.getInt("appWidgetId", 0);
		if (this.mAppWidgetId == 0)
			finish();
		String[] config = loadPref(this, this.mAppWidgetId);
		this.title.setText(config[0]);
		String[] date = config[1].split("-");
		int i = Integer.parseInt(date[0]);
		int j = Integer.parseInt(date[1]);
		int k = Integer.parseInt(date[2]);
		 
		this.datePicker.updateDate(i, j - 1, k);
	}

	private void init() {
		this.title = ((EditText) findViewById(R.id.Type));
		this.datePicker = ((DatePicker) findViewById(R.id.datePicker1));
		this.submitButton = ((Button) findViewById(R.id.submitButton));
		this.submitButton.setOnClickListener(this.submitOnClickListener);
		this.cancelButton = ((Button) findViewById(R.id.cancelBtn));
		this.cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				BaseConf.this.finish();
			}
		});
	}

	View.OnClickListener submitOnClickListener = new View.OnClickListener() {
		public void onClick(View paramView) {
			BaseConf conf = BaseConf.this;
			String str1 = BaseConf.this.title.getText().toString();
			String str2 = BaseConf.this.datePicker.getYear() + "-"
					+ (1 + BaseConf.this.datePicker.getMonth()) + "-"
					+ BaseConf.this.datePicker.getDayOfMonth();
			BaseConf.savePref(conf, BaseConf.this.mAppWidgetId, str1, str2);
			BaseWidget.updateAppWidget(conf,
					AppWidgetManager.getInstance(conf),
					BaseConf.this.mAppWidgetId);
			Intent localIntent = new Intent();
			localIntent.putExtra("appWidgetId", BaseConf.this.mAppWidgetId);
			BaseConf.this.setResult(-1, localIntent);
			BaseConf.this.finish();
		}
	};

	static String[] loadPref(Context context, int paramInt) {
		String[] config = new String[2];
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				"com.zyl.basewidget.BaseWidget", 0);
		config[0] = sharedPreferences.getString("title_" + paramInt, "");
		config[1] = sharedPreferences.getString("date_" + paramInt,
				new SimpleDateFormat("yyyy-MM-dd").format(new Date())
		);
		return config;
	}

	static void savePref(Context context, int widgetId, String title,
			String date) {
		SharedPreferences.Editor editor = context.getSharedPreferences(
				"com.zyl.basewidget.BaseWidget", 0).edit();
		editor.putString("title_" + widgetId, title);
		editor.putString("date_" + widgetId, date);

		editor.commit();
	}

}
